/**
 *
 * Copyright 2015 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
APP.Data = (function() {

	var HN_API_BASE = 'https://hacker-news.firebaseio.com';
	var HN_TOPSTORIES_URL = HN_API_BASE + '/v0/topstories.json';
	var HN_STORYDETAILS_URL = HN_API_BASE + '/v0/item/[ID].json';

	function getTopStories(callback) {
		// test
				var data = [15421704, 15425324, 15425559, 15425162, 15424478, 15425545, 15423674, 15425918, 15424437, 15425374, 15423977, 15424534, 15423202, 15424618, 15426261, 15425906, 15424723, 15423824, 15423384, 15425166, 15426545, 15415207, 15413731, 15416198, 15423045, 15423027, 15424498, 15425962, 15424013, 15422309, 15423171, 15423808, 15417735, 15423816, 15423752, 15423201, 15418671, 15423328, 15425379, 15425829, 15407761, 15409376, 15420770, 15425632, 15422325, 15426061, 15423889, 15417019, 15422157, 15421999, 15422995, 15401211, 15421708, 15423193, 15423699, 15416848, 15417664, 15416819, 15422731, 15417975, 15418832, 15421334, 15425332, 15419860, 15421410, 15420602, 15416006, 15423078, 15421112, 15418109, 15418743, 15424031, 15423107, 15418390, 15413885, 15425068, 15421073, 15414987, 15415992, 15404277, 15417719, 15415978, 15417338, 15408800, 15421487, 15416615, 15414760, 15406724, 15408217, 15417138, 15410969, 15409757, 15410762, 15413289, 15417351, 15418271, 15416892, 15422409, 15418232, 15421403, 15411056, 15422048, 15421463, 15403104, 15400037, 15413753, 15415736, 15395946, 15421384, 15420076, 15414926, 15416932, 15421870, 15415367, 15419619, 15399787, 15424570, 15410259, 15404601, 15422865, 15413377, 15415278, 15400667, 15425199, 15414357, 15408712, 15408209, 15420190, 15399132, 15416384, 15423894, 15414986, 15415510, 15408784, 15420821, 15400396, 15421080, 15408600, 15420718, 15423393, 15419685, 15413709, 15423287, 15414922, 15418719, 15408695, 15412570, 15416002, 15410531, 15416293, 15415818, 15400217, 15394550, 15407098, 15413543, 15417501, 15413785, 15422054, 15409733, 15418840, 15414544, 15404345, 15415597, 15394066, 15422728, 15414166, 15406786, 15417756, 15397798, 15405628, 15423408, 15411261, 15393396, 15408506, 15404203, 15412002, 15398030, 15393126, 15417137, 15410953, 15410983, 15424117, 15406237, 15399297, 15419593, 15408416, 15395347, 15415850, 15422277, 15408180, 15415312, 15403882, 15400873, 15413194, 15399442, 15424393, 15410076, 15405968, 15399855, 15425225, 15420925, 15399835, 15425569, 15415339, 15409898, 15408365, 15411041, 15394603, 15407843, 15423586, 15414055, 15405888, 15400963, 15400436, 15416868, 15402230, 15404838, 15393446, 15408992, 15405510, 15396122, 15395429, 15407164, 15398550, 15410336, 15406338, 15406263, 15412901, 15417164, 15403495, 15403387, 15412686, 15414108, 15413188, 15404970, 15411533, 15399777, 15406173, 15399905, 15410637, 15403813, 15398225, 15411685, 15400285, 15404627, 15423167, 15401397, 15416584, 15407562, 15407804, 15394561, 15423116, 15413036, 15408439, 15405479, 15400319, 15405605, 15403112, 15398005, 15414840, 15399689, 15413018, 15399684, 15410725, 15404954, 15414046, 15397926, 15408914, 15393270, 15418551, 15412173, 15399028, 15408140, 15399108, 15408850, 15402273, 15416375, 15401973, 15413286, 15406826, 15395028, 15415607, 15394791, 15399866, 15396471, 15398595, 15398430, 15414765, 15414170, 15421915, 15404441, 15408194, 15396945, 15412746, 15408826, 15401800, 15402483, 15412762, 15413217, 15404407, 15406714, 15398917, 15421483, 15415726, 15413887, 15400526, 15415699, 15413770, 15408942, 15401761, 15421120, 15417676, 15401276, 15402430, 15417076, 15419715, 15396048, 15401002, 15414851, 15404828, 15397370, 15397532, 15408258, 15414683, 15407226, 15399292, 15400197, 15404602, 15394750, 15414275, 15411463, 15397612, 15395178, 15398804, 15399042, 15421254, 15394744, 15393134, 15423423, 15397008, 15415267, 15400910, 15414266, 15420749, 15414569, 15405074, 15398007, 15395639, 15407515, 15393634, 15406580, 15403134, 15403070, 15409066, 15401971, 15407631, 15411692, 15400850, 15393518, 15401890, 15396643, 15394907, 15400475, 15410834, 15405163, 15395326, 15408635, 15412133, 15393298, 15410106, 15406070, 15393608, 15413290, 15397642, 15418070, 15394551, 15397817, 15395575, 15399799, 15396340, 15401495, 15409241, 15409977, 15402135, 15397509, 15402030, 15413339, 15413084, 15401587, 15398647, 15405803, 15404066, 15396403, 15396199, 15410282, 15405107, 15395217, 15414418, 15415872, 15409683, 15394796, 15397618, 15396087, 15411715, 15402557, 15399138, 15412853, 15405639, 15402439, 15415030, 15405904, 15408623, 15402319, 15403076, 15396681, 15398744, 15395386, 15404584, 15408646, 15397415, 15410936, 15400638, 15404167, 15408071, 15413835, 15407188, 15393674, 15399282, 15395315, 15411003, 15419000, 15397204, 15405914, 15410979, 15410433, 15410108, 15397730, 15406547, 15403943, 15406274, 15406229, 15400627, 15402647, 15402374, 15399957, 15399940, 15393105, 15416332, 15404890, 15393949, 15404704, 15400678, 15396125, 15407178, 15395945, 15395192, 15401610, 15401295, 15401201, 15411006, 15400494, 15396364, 15405971, 15395601, 15405619, 15407956, 15405598, 15405537, 15396952, 15405082, 15404945, 15396548, 15394715, 15403787, 15419437, 15397941, 15397697, 15397541, 15396777, 15396651, 15396592, 15393684, 15398147, 15392929, 15398080, 15398450, 15398283, 15398013, 15397856, 15401461, 15395696, 15400420, 15393528, 15396205, 15402311, 15408144, 15394671, 15411563, 15407218];
				callback(data);
				return;

		request(HN_TOPSTORIES_URL, function(evt) {
			callback(evt.target.response);
		});
	}

	function getStoryById(id, callback) {
		// test
				var data = {
					"by": "misnamed",
					"descendants": 93,
					"id": 15421704,
					"kids": [15425914, 15426166, 15426196, 15426217, 15426003, 15426084, 15426558, 15426116, 15426314, 15426102, 15425901, 15426371, 15426234, 15426400, 15425939, 15426249, 15425883, 15426080, 15426264, 15426150, 15425983],
					"score": 207,
					"time": 1507339510,
					"title": "'Our minds can be hijacked': tech insiders who fear a smartphone dystopia",
					"type": "story",
					"url": "https://www.theguardian.com/technology/2017/oct/05/smartphone-addiction-silicon-valley-dystopia"
				};
		
				callback(data);
				return;

		var storyURL = HN_STORYDETAILS_URL.replace(/\[ID\]/, id);

		request(storyURL, function(evt) {
			callback(evt.target.response);
		});
	}

	function getStoryComment(id, callback) {

		var storyCommentURL = HN_STORYDETAILS_URL.replace(/\[ID\]/, id);

		request(storyCommentURL, function(evt) {
			callback(evt.target.response);
		});
	}

	function request(url, callback) {
		var xhr = new XMLHttpRequest();
		xhr.open('GET', url, true);
		xhr.responseType = 'json';
		xhr.onload = callback;
		xhr.send();
	}

	return {
		getTopStories: getTopStories,
		getStoryById: getStoryById,
		getStoryComment: getStoryComment
	};

})();